package com.cqjtu.eecs.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.cqjtu.eecs.entity.Major;
import com.cqjtu.eecs.entity.Project;
import com.cqjtu.eecs.entity.TrainingGoal;
import com.cqjtu.eecs.entity.params.trainingGoal.TrainingGoalAddParams;
import com.cqjtu.eecs.entity.params.trainingGoal.TrainingGoalUpdateParams;
import com.cqjtu.eecs.entity.vo.TrainingGoalVO;
import com.cqjtu.eecs.exception.ParamsErrorException;
import com.cqjtu.eecs.service.IProjectService;
import com.cqjtu.eecs.service.ITrainingGoalService;
import com.cqjtu.eecs.service.ITrainingGoalTemplateService;
import com.cqjtu.eecs.utils.Assert;
import com.cqjtu.eecs.utils.Result;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 培养目标 前端控制器
 * </p>
 *
 * @author    
 * @since 2022-03-07
 */
@RestController
@RequestMapping("/training-goal")
@Api(tags = "年级培养目标")
public class TrainingGoalController {

    @Autowired
    private ITrainingGoalService trainingGoalService;
    @Autowired
    private IProjectService projectService;
    //通过专业代码和版本编号获取培养目标
    @ApiOperation("通过专业代码和年级查询培养目标")
    @GetMapping("/getTrainingGoalByCodeAndGrade/{majorCode}/{year}")
    public TrainingGoalVO getTrainingGoalVO(@PathVariable("majorCode")String majorCode, @PathVariable("year")String year){
        Assert.notBlank(majorCode, new ParamsErrorException("专业代码不能为空"));
        Assert.notNull(year, new ParamsErrorException("年级不能为空"));
        Project project= projectService.getOne(new QueryWrapper<Project>().eq("major_code",majorCode).eq("grade",year));
        if(project == null)
            return null;
        String pid=project.getPid().toString();
        return trainingGoalService.getTrainingGoalVO(pid);
    }


    @ApiOperation("新增一条培养目标")
    @PostMapping("/addTrainingGoal")
    public Result<Object> addTrainingGoal(@Valid @RequestBody TrainingGoalAddParams trainingGoalAddParams)
    {
        TrainingGoal trainingGoal=new TrainingGoal();
        trainingGoal.setTrainingGoalDesc(trainingGoalAddParams.getTrainingGoalDesc());
        trainingGoal.setTrainingGoalCode(trainingGoalAddParams.getTrainingGoalCode());
        Project project=projectService.getOne(new QueryWrapper<Project>().eq("major_code",trainingGoalAddParams.getMajorCode()).eq("grade",trainingGoalAddParams.getGrade()));
        if(project == null)
            return Result.error(-1,"未查到培养方案");
        int pid=project.getPid();
        trainingGoal.setPid(pid);
        List<TrainingGoal> trainingGoals=trainingGoalService.list(new QueryWrapper<TrainingGoal>().eq("pid",pid));
        for (TrainingGoal i:trainingGoals) {
            if(i.getTrainingGoalCode().equals(trainingGoalAddParams.getTrainingGoalCode()))
                return Result.error(-1,"培养目标代码重复");
        }
        if (trainingGoalService.save(trainingGoal))
            return Result.success();
        return Result.error();
    }

    @ApiOperation("更新一条培养目标")
    @PutMapping("/updateTrainingGoal")
    public Result<Object> updateTrainingGoal(@Valid @RequestBody TrainingGoalUpdateParams trainingGoalUpdateParams)
    {
        TrainingGoal trainingGoal=new TrainingGoal();
        trainingGoal.setTrainingGoalDesc(trainingGoalUpdateParams.getTrainingGoalDesc());
        trainingGoal.setTrainingGoalCode(trainingGoalUpdateParams.getTrainingGoalCode());
        Project project=projectService.getOne(new QueryWrapper<Project>().eq("major_code",trainingGoalUpdateParams.getMajorCode()).eq("grade",trainingGoalUpdateParams.getGrade()));
        if(project == null)
            return Result.error(-1,"未查到培养方案");
        int pid=project.getPid();
        trainingGoal.setPid(pid);

        if (trainingGoalService.update(new LambdaUpdateWrapper<TrainingGoal>()
                .eq(TrainingGoal::getPid, pid)
                .eq(TrainingGoal::getTrainingGoalCode, trainingGoalUpdateParams.getTrainingGoalCode())
                .set(trainingGoal.getTrainingGoalDesc() != null, TrainingGoal::getTrainingGoalDesc, trainingGoal.getTrainingGoalDesc())
        ))
            return Result.success();
        return Result.error(-1,"该培养要求代码不存在");
    }

    @ApiOperation("更新多条培养目标")
    @PutMapping("/updateTrainingGoalList")
    public Result<Object> updateTrainingGoalList(@Valid @RequestBody List<TrainingGoalUpdateParams> trainingGoalUpdateParamsList)
    {
        for (TrainingGoalUpdateParams i:trainingGoalUpdateParamsList
             ) {
            TrainingGoal trainingGoal=new TrainingGoal();
            trainingGoal.setTrainingGoalDesc(i.getTrainingGoalDesc());
            trainingGoal.setTrainingGoalCode(i.getTrainingGoalCode());
            Project project=projectService.getOne(new QueryWrapper<Project>().eq("major_code",i.getMajorCode()).eq("grade",i.getGrade()));
            if(project == null)
                return Result.error(-1,"未查到培养方案");
            int pid=project.getPid();
            trainingGoal.setPid(pid);

            if (trainingGoalService.update(new LambdaUpdateWrapper<TrainingGoal>()
                    .eq(TrainingGoal::getPid, pid)
                    .eq(TrainingGoal::getTrainingGoalCode, i.getTrainingGoalCode())
                    .set(trainingGoal.getTrainingGoalDesc() != null, TrainingGoal::getTrainingGoalDesc, trainingGoal.getTrainingGoalDesc())
            ))
                continue;
            else
                return Result.error();
        }
        return Result.success();
    }

    @ApiOperation("删除一条培养目标")
    @DeleteMapping("/deleteTrainingGoal/{majorCode}/{year}/{trainingGoalCode}")
    public Result<Object> deleteTrainingGoal(@PathVariable("majorCode")String majorCode, @PathVariable("year")String year,@PathVariable("trainingGoalCode")String trainingGoalCode)
    {
        Assert.notBlank(majorCode, new ParamsErrorException("专业代码不能为空"));
        Assert.notBlank(year, new ParamsErrorException("年级不能为空"));
        Assert.notBlank(trainingGoalCode,new ParamsErrorException("培养目标代码不能为空"));
        Project project=projectService.getOne(new QueryWrapper<Project>().eq("major_code",majorCode).eq("grade",year));
        if(project == null)
            return Result.error(-1,"未查到培养方案");
        int pid=project.getPid();

        if (trainingGoalService.remove(new LambdaQueryWrapper<TrainingGoal>().eq(TrainingGoal::getPid, pid).eq(TrainingGoal::getTrainingGoalCode,trainingGoalCode)))
            return Result.success();
        return Result.error(-1,"该培养要求代码不存在");
    }
}
